package com.cea.papyrus.diagram.classdiagram.action;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPart;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.classdiagram.editpart.ClassEditPart;

public class AddAllFeatures extends org.eclipse.gef.ui.actions.SelectionAction {
	/**
	 * 
	 */
	public final static String ADD_ALL_FEATURE_ACTION_REQUEST = AddAllFeatures.class.getName()+ "Request";

	/**
	 * 
	 */
	public final static String ADD_ALL_FEATURE_ACTION_ID = AddAllFeatures.class.getName()+ "ID";

	/**
	 * Request created by this action.
	 */
	protected Request request;

	/**
	 * Default constructor.
	 * 
	 * @param part 
	 */
	public AddAllFeatures(IWorkbenchPart part) {
		super(part);
		request = new Request(ADD_ALL_FEATURE_ACTION_REQUEST);
		setText("Add all features");
		setId(ADD_ALL_FEATURE_ACTION_ID);
		setToolTipText("Add all properties and operations");

		// icon for action
		ImageDescriptor descriptor = PapyrusPlugin.getImageDescriptor(PapyrusPlugin.PAPYRUS_ICONS_16x16+"Add_16x16.gif");
		setImageDescriptor(descriptor);
		setHoverImageDescriptor(descriptor);

	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected boolean calculateEnabled() {

		return canPerformAction();
	}	

	/**
	 * 
	 * 
	 * @return 
	 */
	private boolean canPerformAction() {

		if (getSelectedObjects().isEmpty()) {
			return false;
		}
		List parts = getSelectedObjects();
		for (int i=0; i<parts.size(); i++){
			Object o = parts.get(i);
			if (!(o instanceof ClassEditPart)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	private Command getCommand() {
		List editparts = getSelectedObjects();
		CompoundCommand cc = new CompoundCommand();
		Debug.debug(this,"Action::getCommand()"+editparts.size(), 21);
		for (int i=0; i < editparts.size(); i++) {
			EditPart part = (EditPart) editparts.get(i);
			if( part.getCommand(request).canExecute()){
				cc.add(part.getCommand(request));}
		}
		displayCompoundCommand(cc);
		return cc;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	/**
	 * 
	 */
	@Override
	public void run () {
		execute(getCommand());
	}

	/**
	 * 
	 * 
	 * @param selection 
	 */
	@Override
	public void setSelection(ISelection selection) {
		super.setSelection(selection);
	}

	/**
	 * 
	 * 
	 * @param command 
	 */
	void displayCompoundCommand(CompoundCommand command) {
		Iterator<Command> it = command.getCommands().iterator();
		while(it.hasNext()) {
			displayCommand(it.next());
		}
	}

	/**
	 * 
	 * 
	 * @param command 
	 */
	void displayCommand(Command command) {
		if(command instanceof CompoundCommand) {
			displayCompoundCommand((CompoundCommand)command);
		}
		else {
			Debug.debug(this, command.toString(), 21);
		}
	}
}
